<template>
  <base-breadcrumb>
    <div class="about-master-container">
      <b-steps
        :current="stepVal"
        direction="vertical"
        @change.self="stepChange"
      >
        <b-step title="基本信息">
          <a-card
            v-show="activeKey[0]"
            slot="description"
            v-loading="loading"
            :bordered="false"
          >
            <a-form-model
              ref="ruleForm"
              :model="formData"
              :rules="rules"
            >
              <a-row>
                <a-col span="8">
                  <a-form-model-item
                    label="供应商考察编码"
                    prop="supplierSurveyCode"
                  >
                    <a-input-search
                      v-model="formData.supplierSurveyCode"
                      placeholder="请选择"
                      :disabled="state === 3"
                      @search="openInspection"
                      @click="openInspection"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="供应商名称"
                    prop="name"
                  >
                    <BaseInput
                      v-model="formData.name"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="供应商编码"
                    prop="code"
                  >
                    <BaseInput
                      v-model="formData.code"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="纳税人类型"
                    prop="taxpayerTypeId"
                  >
                    <a-select
                      v-model="formData.taxpayerTypeId"
                      disabled
                    >
                      <a-select-option
                        v-for="item in typeArr"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="适用税率%"
                    prop="taxRateTypeId"
                  >
                    <BaseInput
                      v-model="formData.taxRateType"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="公司性质"
                    prop="natureTypeId"
                  >
                    <a-select
                      v-model="formData.natureTypeId"
                      placeholder="请选择"
                      :disabled="state === 3"
                    >
                      <a-select-option
                        v-for="item in natureArr"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="成立日期"
                    prop="registrationDate"
                  >
                    <a-date-picker
                      v-model="formData.registrationDate"
                      value-format="YYYY-MM-DD"
                      :disabled="isDisabled"
                      format="YYYY-MM-DD"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="通讯地址"
                    prop="address"
                  >
                    <BaseInput
                      v-model="formData.address"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="公司网址"
                    prop="website"
                  >
                    <BaseInput
                      v-model="formData.website"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="营业执照号"
                    prop="creditCode"
                  >
                    <BaseInput
                      v-model="formData.creditCode"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>

                <a-col span="8">
                  <a-form-model-item
                    label="法定代表人"
                    prop="legalRepresentative"
                  >
                    <BaseInput
                      v-model="formData.legalRepresentative"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="注册资金（元）"
                    prop="registeredCapital"
                  >
                    <BaseInput
                      v-model="formData.registeredCapital"
                      type="number"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="公司员工总数"
                    prop="employeeSize"
                  >
                    <BaseInput
                      v-model="formData.employeeSize"
                      type="number"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="传真"
                    prop="fax"
                  >
                    <BaseInput
                      v-model="formData.fax"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="联系人"
                    prop="contacts"
                  >
                    <BaseInput
                      v-model="formData.contacts"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="联系电话"
                    prop="phone"
                  >
                    <BaseInput
                      v-model="formData.phone"
                      type="number"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="开户银行"
                    prop="bankAccount"
                  >
                    <BaseInput
                      v-model="formData.bankAccount"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="银行账号"
                    prop="bankAccountNum"
                  >
                    <BaseInput
                      v-model="formData.bankAccountNum"
                      type="number"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="准入机构"
                    prop="accessOrg"
                  >
                    <a-select
                      v-model="formData.accessOrg"
                      placeholder="请选择"
                      :disabled="isDisabled"
                    >
                      <a-select-option
                        v-for="item of selectOptionList.accessOrg"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.label }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="所属机构"
                    prop="ownOrg"
                  >
                    <a-select
                      v-model="formData.ownOrg"
                      placeholder="请选择"
                      :disabled="isDisabled"
                      mode="multiple"
                    >
                      <a-select-option
                        v-for="item of selectOptionList.ownOrg"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.label }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="统一社会信用代码"
                    prop="creditNum"
                  >
                    <BaseInput
                      v-model="formData.creditNum"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="邮箱"
                    prop="email"
                  >
                    <BaseInput
                      v-model="formData.email"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="公司所在地"
                    prop="companyAddress"
                  >
                    <BaseInput
                      v-model="formData.companyAddress"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="分类"
                    prop="supTypeId"
                  >
                    <a-select
                      v-model="formData.supTypeId"
                      placeholder="请选择"
                      :disabled="isDisabled"
                    >
                      <a-select-option
                        v-for="item of selectOptionList.type"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="本级评分"
                    prop="supScore"
                  >
                    <BaseInput
                      v-model="formData.supScore"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="评级"
                    prop="supGrade"
                  >
                    <BaseInput
                      v-model="formData.supGrade"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="法人代表联系方式"
                    prop="legalContact"
                  >
                    <BaseInput
                      v-model="formData.legalContact"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="24">
                  <a-form-model-item
                    label="公司简介"
                    prop="introduction"
                  >
                    <BaseTextarea
                      v-model="formData.introduction"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="24">
                  <a-form-model-item
                    label="产品特色"
                    prop="featureOfProduct"
                  >
                    <BaseTextarea
                      v-model="formData.featureOfProduct"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="24">
                  <a-form-model-item
                    label="供货能力"
                    prop="supplyCapacity"
                  >
                    <BaseTextarea
                      v-model="formData.supplyCapacity"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="24">
                  <a-form-model-item
                    label="主营业务"
                    prop="mainBusiness"
                  >
                    <BaseTextarea
                      v-model="formData.mainBusiness"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="24">
                  <a-form-model-item
                    label="主要经营区域"
                    prop="businessArea"
                  >
                    <BaseTextarea
                      v-model="formData.businessArea"
                      :disabled="isDisabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="考察时间">
                    <a-date-picker
                      v-model="formData.surveyDate"
                      value-format="YYYY-MM-DD"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建人">
                    <BaseInput
                      v-model="formData.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建时间">
                    <BaseInput
                      v-model="formData.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
            <div class="pub-next-btn">
              <a-button
                v-if="state !== 3&&hideBuquBtn()"
                type="primary"
                @click="temporaryStorage()"
              >
                保存草稿
              </a-button>
              <a-button
                type="primary"
                @click="temporaryStorage(true)"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="相关附件">
          <a-card
            v-if="activeKey[1]"
            slot="description"
            :bordered="false"
          >
            <u-table
              :upload-list="addFile"
              :delete-url="fileDel"
              :is-show="state !== 3"
              :list-url="fileList"
              :business-id="row.id"
              business-code="MM19"
            />
          </a-card>
        </b-step>
      </b-steps>
      <div class="pub-header-btn page-btn-right-top">
        <a-button
          v-if="state !== 3&&hideBuquBtn()"
          type="primary"
          :loading="loading"
          @click="submit"
        >
          提交
        </a-button>
        <a-button @click="handleClose">
          关闭
        </a-button>
      </div>
    </div>
    <inspection
      ref="inspection"
      @getInspection="getInspection"
    />
  </base-breadcrumb>
</template>
<script>
import inspection from '../components/inspection/inspection'
import { supplierAdd, supplierEdit, audit, addFile, fileList, fileDel } from '@/api/material/supplier/supplier'
import { supplierLabel } from '@/api/material/public/public'
import totalPackageContractApi from '@/api/legal/branchContract/totalPackageContract'
import { faxRules,phoneRules,emailRules,creditCodeRules } from '@/utils/regular'
import { getCustOrgTree } from '@/api/subassembly/TreeSelectOrganization'
export default {
    name: 'a' + Date.now(),
    components: {
        inspection
    },
    data() {
        return {
            activeKey: [true, false, false, false, false, false, false],
            loading: false,
            stepVal: 0,
            state: Number(this.$route.query.state),
            isSubmit: false,
            row: this.$route.query.row,
            formData: {
                createTime: this.getNowTime(),
                creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
                supplierSurveyCode: '',
                supplierTypeEnum: 'SUPPLIER_ADD',
                name: '',
                code: '',
                taxpayerTypeId: null,
                natureTypeId: void 0,
                registrationDate: null,
                address: '',
                website: '',
                creditCode: '',
                mainBusiness: '',
                legalRepresentative: '',
                registeredCapital: '',
                employeeSize: '',
                fax: '',
                contacts: '',
                phone: '',
                bankAccount: '',
                bankAccountNum: '',
                introduction: '',
                featureOfProduct: '',
                supplyCapacity: '',
                // surveyDate: this.getNowTime(),
                surveyDate: '',
                supplierSurveyId: '',
                supplierType: '',
                status: 1,

                accessOrg: void 0,
                ownOrg: void 0,
                supTypeId: void 0
            },

            rules: {
                supplierSurveyCode: { required: true, message: '供应商考察编码不能为空', trigger: 'change' },
                natureTypeId: { required: true, message: '公司性质不能为空', trigger: 'blur' },
                registrationDate: { required: true, message: '成立日期不能为空', trigger: 'change' },
                address: { required: true, message: '通讯地址不能为空', trigger: 'change' },
                creditCode: { required: true, message: '营业执照号不能为空', trigger: 'change' },
                mainBusiness: { required: true, message: '主营业务不能为空', trigger: 'change' },
                legalRepresentative: { required: true, message: '法定代表人不能为空', trigger: 'change' },
                registeredCapital: { required: true, message: '注册资金（元）不能为空', trigger: 'change' },
                employeeSize: { required: true, message: '公司员工总数不能为空', trigger: 'change' },
                // fax: { required: true, message: '传真不能为空', trigger: 'change' },
                fax: [{ required: true, validator: faxRules, trigger: 'change' }],
                contacts: { required: true, message: '联系人不能为空', trigger: 'change' },
                website: { required: true, message: '公司网址不能为空', trigger: 'change' },
                phone: { required: true, validator: phoneRules,  trigger: 'change' },
                bankAccount: { required: true, message: '开户银行不能为空', trigger: 'change' },
                bankAccountNum: { required: true, message: '银行账号不能为空', trigger: 'change' },

                accessOrg: { required: true, message: '准入机构不能为空', trigger: 'change' },
                ownOrg: { required: true, message: '所属机构不能为空', trigger: 'change' },
                creditNum: { required: true, validator: creditCodeRules, trigger: 'change' },

                email: { required: true, validator: emailRules, trigger: 'change' },
                companyAddress: { required: true, message: '公司所在地不能为空', trigger: 'blur' },
                businessArea: { required: true, message: '主要经营区域不能为空', trigger: 'blur' },
                supTypeId: { required: true, message: '分类不能为空', trigger: 'change' },
                supScore: { required: true, message: '本级评分不能为空', trigger: 'blur' },
                supGrade: { required: true, message: '评级评分不能为空', trigger: 'blur' },
                legalContact: { required: true, message: '法人代表联系方式不能为空', trigger: 'blur' }
            },
            isNext: false,
            addFile: addFile(),
            fileDel: fileDel(),
            fileList: fileList(),
            isDisabled: false,
            typeArr: [],
            natureArr: [],
            typeOptions: [],
            selectOptionList: {
                accessOrg: [],
                ownOrg: []
            }
        }
    },
    watch: {
        stepVal(newVal, oldVal) {
            this.$set(this.activeKey, newVal, true)
        }
    },
    mounted:async  function () {
        this.getType()
        this.getNature()
        this.getOptions('type', 18)

        // let levels = JSON.parse(sessionStorage.getItem('ycloud-user_info')).employee.levels
        // this.selectOptionList.ownOrg = this.selectOptionList.accessOrg = levels.filter(
        //     (v) => v.code === 'CO' || v.code === 'BC'
        // )
      let params = {
        containTags:['CO','BC'],
        baseOrgType:0,
        systemOrgType: 1,
        containVirtual: 1,
       }
      let res = await getCustOrgTree(params)
      if (res.data){
        // console.log('ressssssssss',res.data[0])
        let temp = res.data[0].children
        temp.push({id:res.data[0].id,label:res.data[0].label})
        this.selectOptionList.ownOrg = this.selectOptionList.accessOrg =temp
      }
        this.isDisabled = this.state === 3
        if (this.state && this.state !== 1) {
            this.getEditInfo()
            this.isSubmit = true
        }
    },
    methods: {
        async getOptions(key, type) {
            let res = await supplierLabel({ type })
            if (res.code === 200) {
                this.$set(this.selectOptionList, key, res.data)
            }
        },
        handleClose() {
            this.$close()
        },
        previous: function () {
            this.isNext = true
            this.stepVal--
            if (this.stepVal < 0) {
                this.stepVal = 0
            }
        },
          getInspection: function (row) {
            this.$console(row)
            this.formData.supplierSurveyCode = row.surveyCode
            this.formData.name = row.surveyObj
            this.formData.taxpayerTypeId = row.taxpayerType
            this.formData.taxRateType = row.taxRateType
            this.formData.introduction = row.introduction
            this.formData.featureOfProduct = row.featureOfProduct
            this.formData.supplyCapacity = row.supplyCapacity
            this.formData.surveyDate = row.surveyDate
            this.formData.supplierSurveyId = row.id
            this.formData.status = row.status
        },
        getType: function () {
            let _this = this
            supplierLabel({
                type: 9
            }).then((res) => {
                if (res.code === 200) {
                    _this.typeArr = res.data
                }
            })
        },
        getNature: function () {
            let _this = this
            supplierLabel({
                type: 0
            }).then((res) => {
                if (res.code === 200) {
                    _this.natureArr = res.data
                }
            })
        },
        openInspection: function () {
            this.$refs.inspection.open()
        },
        stepChange: function (val) {
            if (!this.row) {
                if (val) {
                    this.$message.warning('切换步骤前请先填写基本信息')
                    this.stepVal = 0
                }
            } else {
                if (!this.isNext) {
                    this.$set(this.activeKey, val, !this.activeKey[val])
                }
            }
            this.isNext = false
        },
        // 获取编辑回显数据
        getEditInfo() {
            this.formData = this.row
          this.formData.ownOrg =  this.formData.ownOrg.split(',')
        },
        submit() {
            // this.save('approve');
            if (this.isSubmit) {
                this.$submitConfirm(() => {
                    this.loading = true
                    audit({
                        baseId: this.row.id,
                        auditStatus: this.row.auditStatus
                    }).then((res) => {
                        this.$message.success('提交成功')
                        this.loading = false
                        this.$router.push({ path: '/material/supplier/new' })
                    })
                })
            } else {
                this.$message.warning('提交之前请先暂存')
            }
        },
        temporaryStorage(isNext) {
            if (this.state === 3) {
                this.next()
                return
            }
            this.$console(this.formData)
            this.$refs.ruleForm.validate((valid, object) => {
                if (valid) {
                    this.loading = true
                  let temp = this.$clone(this.formData)
                  temp.ownOrg = temp.ownOrg.join(',')
                  if (this.state === 2 || this.row) {
                      temp.id = this.row.id
                      temp.supplierType = 'SUPPLIER_ADD'
                        supplierEdit(temp).then((res) => {
                            if (res.code === 200) {
                                this.$message.success('暂存成功')
                                this.isSubmit = true
                                if (isNext) {
                                    this.next()
                                }
                            }
                            this.loading = false
                        })
                    } else {
                    temp.supplierType = 'SUPPLIER_ADD'
                        supplierAdd(temp).then((res) => {
                            if (res.code === 200) {
                                this.row = res.data
                                this.$message.success('暂存成功')
                                this.isSubmit = true
                                this.getEditInfo()
                                if (isNext) {
                                    this.next()
                                }
                            }
                            this.loading = false
                        })
                    }
                } else {
                  this.$message.warning("请填写完所有必填项信息")
                    return false
                }
            })
        },
        next: function (val) {
            this.isNext = true
            this.stepVal++
        }
    }
}
</script>
